package com.rongxun.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongxun.base.BaseObserver;
import com.rongxun.base.IBase;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.model.bean.User;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.utils.ALog;
import com.rongxun.utils.MyToast;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;

/**
 * 创建者     庄丰泽
 * 创建时间   2018/8/9 14:58
 * 描述	     兑换Token
 */
public class ExchangeTokenDialog extends Dialog {
    private static CompositeDisposable disposable;
    private static Dialog mLoadingDialog;

    public ExchangeTokenDialog(@NonNull Context context) {
        super(context);
    }

    public ExchangeTokenDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements TextWatcher {
        private Context context;
        private ExchangeTokenDialog dialog;
        private Unbinder bind;
        private ApiModel apiModel;
        private int mini_exchange;//最小兑换值
        private int ratio_exchange;//兑换比例
        private double userCandyNum;
        @BindView(R.id.ed_Token_exchange_number)
        EditText ed_token_number;
        @BindView(R.id.dialog_exchange_token_number)
        TextView tv_token_number;
        @BindView(R.id.tv_convertible_exchange_number)
        TextView tv_convertible_number;
        private IBase.IView mview;
        public Builder(Context context,double userCandyNum,int mini_exchange, int ratio_exchange,final IBase.IView view) {
            this.context = context;
            this.mini_exchange = mini_exchange;
            this.ratio_exchange = ratio_exchange;
            this.mview=view;
            this.userCandyNum=userCandyNum;
            disposable = new CompositeDisposable();
            mLoadingDialog = LoadingDialog.createLoadingDialog(context, "加载中", false);
            apiModel = new ApiModel(context);
        }
        public ExchangeTokenDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dialog = new ExchangeTokenDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialong_exchange_token, null);
            bind = ButterKnife.bind(this, layout);
            tv_token_number.setText(String.format(context.getString(R.string.exchange_token_number),"0"));
            tv_convertible_number.setText(String.format(context.getString(R.string.sweet_number),String.valueOf(userCandyNum/1000)));
            ed_token_number.addTextChangedListener(this);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            return dialog;
        }
        private void empty(){
            disposable.dispose();
            bind.unbind();
            mview=null;
        }
        @OnClick({R.id.dialog_but_cancel, R.id.dialog_but_affirm})
        public void onViewClicked(View view) {
            if (view.getId() == R.id.dialog_but_cancel) {
                if (disposable != null) {
                    empty();
                }
                dialog.dismiss();
            } else if (view.getId() == R.id.dialog_but_affirm) {
                if(ed_token_number.getText().toString().isEmpty()){
                    MyToast.showToast(context,context.getString(R.string.please_input_number));
                    return;
                }
                int input_number = Integer.valueOf(ed_token_number.getText().toString());
                ALog.i("input_number" + input_number + "mini_exchange" + mini_exchange);
                if (mini_exchange > input_number) {
                    MyToast.showToast(context, String.format(context.getString(R.string.exchange_token), mini_exchange));
                    return;
                }
                disposable.add(apiModel.ChangeSweet(Integer.valueOf(ed_token_number.getText().toString())).subscribeWith(new BaseObserver<User>(mview) {
                    @Override
                    protected void onStart() {
                        super.onStart();
                        mLoadingDialog.show();
                    }
                    @Override
                    public void onSuccess(User user, Context c) {
                        MyToast.showToast(context,context.getString(R.string.exchange_succeed));
                        mLoadingDialog.dismiss();
                        empty();
                    }
                    @Override
                    public void onFailure(int code, String message) {
                        super.onFailure(code, message);
                        mLoadingDialog.dismiss();
                    }
                }));
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(!ed_token_number.getText().toString().isEmpty()){
                String number=String.valueOf((Double.valueOf(ed_token_number.getText().toString())/ratio_exchange));
                tv_token_number.setText(String.format(context.getString(R.string.exchange_token_number),number));
            }else{
                tv_token_number.setText(String.format(context.getString(R.string.exchange_token_number),"0"));
            }
        }
        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
